using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
using com.petiteappli.xenocide;

namespace Premier_test
{
    class deprecated
    {
    }
    public class MO_Radar
    {
        float[] mData;
        float mRange;
        float mAlpha;
        String mName;
        World mWorld;
        ManualObject mManualObject;
        MobileActor mParent;
        Queue <int> mRecentPings;
        //Material mMaterial;
        int mPingHistorySize;
        int mCurrentDirection;
        int mAngleStep;
        Vector3 mRelativePos;
        Vector3[] mRadVect;
		
	
    		public MO_Radar(String iName, float iRange,MobileActor iParent, float iRelativeHeight)
		{
		
			mParent=iParent;
			mWorld=iParent.ContainingWorld;
            mName = iName;
			mRange=iRange;
            mAngleStep = 5;
			mData=new float[360/mAngleStep];
            mRadVect=new Vector3[360 / mAngleStep];
			mAlpha=0.5f;
            mPingHistorySize = 25;
            mCurrentDirection = 0;
            mManualObject = mWorld.MainWindow.SceneMgr.CreateManualObject(iName);
            mRelativePos = Vector3.UNIT_Y*iRelativeHeight;
			mManualObject.Dynamic=true;
			
			mManualObject.EstimateVertexCount((uint)mData.Length+1);
			mManualObject.EstimateIndexCount((uint)mData.Length+2);
			
			/** ESSAI pour la transparence*/
			mManualObject.RenderQueueGroup=(byte)(RenderQueueGroupID.RENDER_QUEUE_OVERLAY-1);
			//mManualObject.UseIdentityProjection=true;
			
			//mManualObject.UseIdentityView=true;
			mManualObject.CastShadows=true;


            // create material (colour)
            MaterialPtr moMaterial = MaterialManager.Singleton.Create("line_material", "debugger");
            moMaterial.ReceiveShadows = false;
            moMaterial.GetTechnique(0).SetLightingEnabled(false);
            moMaterial.GetTechnique(0).GetPass(0).SetDiffuse(0, 0, 1, 0);
            moMaterial.GetTechnique(0).GetPass(0).SetAmbient(0, 0, 1);
            moMaterial.GetTechnique(0).GetPass(0).SetSelfIllumination(0, 0, 1);
            
           
			// Attention, charge un Material equivalent !
			// create material (colour)
            //Pair<Mogre.ResourcePtr, bool> p = MaterialManager.Singleton.CreateOrRetrieve("Examples/Rockwall", "Radar_Group");
            
            // create material (colour)
            // create line object
            /*ManualObject manOb = Smgr.CreateManualObject("line");
            manOb.Begin("line_material", RenderOperation.OperationTypes.OT_LINE_LIST);
            manOb.Position(30, 20, 10);
            manOb.Position(40, 10, 0);
            // ... maybe more points
            manOb.End();
             */
             
            //mMaterial = new Material(MaterialManager.Singleton,"Radar",0,"RadarGroup",true);
            //mMaterial = pMat;
            /*
            { 
				mMaterial.ReceiveShadows = false;
				mMaterial.GetTechnique(0).SetLightingEnabled(true);
				mMaterial.GetTechnique(0).GetPass(0).SetDiffuse(0, 1, 0, 0);
				mMaterial.GetTechnique(0).GetPass(0).SetAmbient(0, 1, 0);
				mMaterial.GetTechnique(0).GetPass(0).SetSelfIllumination(0, 1, 0);
			}	
			*/
            //mManualObject..SetMaterialName(0, "Examples/Rockwall");
            mManualObject.Dynamic = true;
            //mManualObject.Begin("line_material", RenderOperation.OperationTypes.OT_TRIANGLE_FAN);
            mManualObject.Begin("BaseWhiteNoLighting", RenderOperation.OperationTypes.OT_LINE_STRIP);
			mManualObject.Position(0.0f,0.0f,0.0f);
			mManualObject.Colour(0.0f,0.5f,0.0f,mAlpha);
			mManualObject.Visible = true;
			
			float rad,offset;
			
			for (int i=0;i<mData.Length;i++)
			{
				rad=Mogre.Math.DegreesToRadians(i*mAngleStep);
                mRadVect[i] = new Vector3(Mogre.Math.Cos(-rad), 0.0f, Mogre.Math.Sin(-rad));
                mManualObject.Position(mRadVect[i]);
				offset=(i*1.0f)/mData.Length;
                mManualObject.Colour(0.0f, Mogre.Math.Abs(180.0f-i*1.0f)/180.0f*0.5f+0.5f,   0.0f, mAlpha);
                //mManualObject.Colour(1,1,1,1);
			}
			
			mManualObject.Index(0);
            mData[0] = 0.0f;
			for (int i=1;i<mData.Length;i++)
			{
				mManualObject.Index((ushort)(i+1));
				mData[i]=1.0f;
				
			}
			mManualObject.Index(1);
			mManualObject.End();

            /*
             // create line object
            ManualObject manOb = mWorld.MainWindow.SceneMgr.CreateManualObject("line");
            manOb.Begin("line_material", RenderOperation.OperationTypes.OT_LINE_LIST);
            manOb.Position(30, 20, 10);
            manOb.Position(40, 10, 0);
            // ... maybe more points
            manOb.End();
            */
			// Use infinite AAB to always stay visible
			AxisAlignedBox aabInf=new AxisAlignedBox();
			aabInf.SetInfinite();
			mManualObject.BoundingBox=aabInf;
			// Render just before overlays
            //mManualObject.RenderQueueGroup = 1;
                ;
		
			mRecentPings=new Queue<int>(25);

		
		}
		
		public void scanSector(int iBegin,int iEnd)
		{
			int i;
			
			iBegin=reCenter(iBegin);
			iEnd=reCenter(iEnd);
			
			for (i=iBegin;i!=iEnd;i++)
			{
                i=reCenter(i);
                ping(i);
			}
            refreshGeometry();
		
		
		}
		int reCenter(int iAngle)
		{
			iAngle=iAngle%360;
			while (iAngle<0) iAngle+=360;
            return iAngle;
		}

        public void update()
        {
            ping(mCurrentDirection);
            refreshGeometry();
            mCurrentDirection = reCenter(mCurrentDirection + 1);
            

        }

		
		public float ping(int iAngle)
		{
			iAngle=reCenter(iAngle);
            int iAngleIndex = iAngle / mAngleStep;
			
			float angle=Mogre.Math.DegreesToRadians(iAngle);
			Quaternion q=new Quaternion(angle,Vector3.UNIT_Y);
			Vector3 direction=q*Vector3.UNIT_X;

			// Bricolage
            float dist = mRange;
            if (mParent.ContainingWorld.MainWindow.queryTerrain(mParent.Position+mRelativePos, ref direction))
            {
                dist = (mParent.Position - direction).Length;
            }
			

			if (dist<mRange)
                mData[iAngleIndex] = dist / mRange;
				else
                mData[iAngleIndex] = 1.0f;
			
			if (mRecentPings.Count>=mPingHistorySize)
                 mRecentPings.Dequeue();
			mRecentPings.Enqueue(iAngle);
			
			return dist;
		}
		
		/** Comment faire ?
		*/
		void refreshGeometry()
		{
            mManualObject.BeginUpdate(0);
            
                mManualObject.Position(0.0f, 0.0f, 0.0f);
                
                
                for (int i = 1; i < mData.Length; i++)
                {
                    mManualObject.Position(mRadVect[i]*mData[i]);
                    
                }
                //mManualObject.Position(mRadVect[1] * mData[1]);
                
                //mManualObject.Position(0.0f, 0.0f, 0.0f);

                mManualObject.Index(0);
               for (int i = 1; i < mData.Length; i++)
                mManualObject.Index((ushort)(i + 1));
               mManualObject.Index(1);
            mManualObject.End();
			
		}
        #region Properties
        public ManualObject SceneObject
        {
            get { return mManualObject; }
        }

        public float Range
        {
            get { return mRange; }
            set { mRange = Mogre.Math.Abs(value); }
        }
        #endregion

 
		
	}
}
